package at.ac.univie.zsu.aguataplan.ui;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.domain.Club;
import at.ac.univie.zsu.aguataplan.domain.ClubEMail;
import at.ac.univie.zsu.aguataplan.domain.ClubWeb;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.service.ClubManager;
import at.ac.univie.zsu.aguataplan.ui.template.IDialogListEntry;
import at.ac.univie.zsu.aguataplan.ui.template.ListEntryPanel;

/**
 * Dialog to admin club
 * 
 * TODO: better exception handling (different messages,...)
 * 
 * @author gerry
 * 
 */
public class DialogClub extends JDialog implements UIConstants {

	private static final long serialVersionUID = 1L;

	private JFrame parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;

	private ClubManager clubManager;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogClub(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.clubManager = parent.getServices().getClubManager();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(SPACING + PANEL_SIZE_X_ENTRY + SPACING + PANEL_SIZE_X_ENTRY + SPACING + SPACING, SPACING
				+ PANEL_SIZE_Y_ENTRY + SPACING + DIALOG_SIZE_Y_HEADER);
		setTitle(i18nProp.getString(CLUB_HEADER));
		setResizable(false);

		addEntries();
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {
		// club panel
		final ListEntryPanel clubPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<?> initText() {
				List<Club> clubs = null;
				try {
					clubs = clubManager.getClubList();
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_LIST_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return clubs;
			}

			@Override
			public List<String> select(Object obj) {
				Club club = (Club) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(club.getName()));
				return list;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				Club club = null;
				try {
					club = clubManager.addClub(list.get(0));
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_ADD_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return club;
			}

			@Override
			public Object delete(Object obj) {
				Club club = (Club) obj;

				try {
					clubManager.deleteClub(club.getId());
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_DELETE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

					return null;
				}

				return club;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				Club club = (Club) obj;
				club.setName(list.get(0));

				Club c = null;

				try {
					c = clubManager.mergeClub(club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_SAVE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}

				return c;
			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});
		List<String> listClub = new ArrayList<String>();
		listClub.add(new String(i18nProp.getString(CLUB_NAME)));
		clubPanel.init(i18nProp.getString(CLUB_BORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY, listClub,
				new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<ActionListener>());

		// clubEMail panel
		ListEntryPanel clubEMailPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				ClubEMail clubEMail = (ClubEMail) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(clubEMail.getAddress()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				ClubEMail clubEMail = (ClubEMail) obj;
				clubEMail.setAddress(list.get(0));

				ClubEMail c = null;

				try {
					c = clubManager.mergeClubEMail(clubEMail);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_SAVE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}

				return c;

			}

			@Override
			public List<?> initText() {
				Club club = (Club) clubPanel.getSelectedValueEntry();

				List<ClubEMail> list = null;
				if (club == null) {
					return null;
				}
				try {
					list = clubManager.getClubEMailList(club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_LIST_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return list;
			}

			@Override
			public Object delete(Object obj) {
				ClubEMail clubEMail = (ClubEMail) obj;

				try {
					clubManager.deleteClubEMail(clubEMail);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_DELETE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

					return null;
				}
				return clubEMail;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				Club club = (Club) clubPanel.getSelectedValueEntry();

				if (club == null) {
					return null;
				}
				ClubEMail clubEMail = club.addEMail(list.get(0));

				try {
					club = clubManager.mergeClub(club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_ADD_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return clubEMail;

			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});
		List<String> listClubEMail = new ArrayList<String>();
		listClubEMail.add(new String(i18nProp.getString(CLUB_EMAILADDRESS)));
		clubEMailPanel.init(i18nProp.getString(CLUB_EMAILBORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY_THIRD,
				listClubEMail, new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<ActionListener>());

		// clubWeb panel
		ListEntryPanel clubWebPanel = new ListEntryPanel(this, generalProperties, new IDialogListEntry() {

			@Override
			public List<String> select(Object obj) {
				ClubWeb clubWeb = (ClubWeb) obj;
				List<String> list = new ArrayList<String>();
				list.add(new String(clubWeb.getAddress()));
				return list;
			}

			@Override
			public Object merge(Object obj, List<String> list, List<Object> combos, List<Date> dates) {
				ClubWeb clubWeb = (ClubWeb) obj;
				clubWeb.setAddress(list.get(0));

				ClubWeb c = null;

				try {
					c = clubManager.mergeClubWeb(clubWeb);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_SAVE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}

				return c;
			}

			@Override
			public List<?> initText() {
				Club club = (Club) clubPanel.getSelectedValueEntry();

				List<ClubWeb> list = null;
				if (club == null) {
					return null;
				}
				try {
					list = clubManager.getClubWebList(club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_LIST_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return list;
			}

			@Override
			public Object delete(Object obj) {
				ClubWeb clubWeb = (ClubWeb) obj;

				try {
					clubManager.deleteClubWeb(clubWeb);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_DELETE_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

					return null;
				}
				return clubWeb;
			}

			@Override
			public Object add(List<String> list, List<Object> combos, List<Date> dates) {
				Club club = (Club) clubPanel.getSelectedValueEntry();

				if (club == null) {
					return null;
				}
				ClubWeb clubWeb = club.addWeb(list.get(0));

				try {
					club = clubManager.mergeClub(club);
				} catch (DatabaseException e) {
					JOptionPane.showMessageDialog(DialogClub.this, i18nProp.getString(EX_CLUB_ADD_DESC),
							i18nProp.getString(EX_CLUB), JOptionPane.ERROR_MESSAGE);

				}
				return clubWeb;
			}

			@Override
			public List<List<?>> initCombo() {
				return null;
			}

			@Override
			public List<Integer> selectCombo(Object obj) {
				return null;
			}

			@Override
			public List<Date> initDate() {
				return null;
			}

			@Override
			public List<Date> selectDate(Object obj) {
				return null;
			}

		});
		List<String> listClubWeb = new ArrayList<String>();
		listClubWeb.add(new String(i18nProp.getString(CLUB_WEBADDRESS)));
		clubWebPanel.init(i18nProp.getString(CLUB_WEBBORDER), PANEL_SIZE_X_ENTRY, PANEL_SIZE_Y_ENTRY_THIRD,
				listClubWeb, new ArrayList<String>(), new ArrayList<String>(), new ArrayList<String>(),
				new ArrayList<ActionListener>());

		// set relationship
		clubPanel.addChild(clubEMailPanel);
		clubPanel.addChild(clubWebPanel);
		clubEMailPanel.addParent(clubPanel);
		clubWebPanel.addParent(clubPanel);

		// add to dialog
		clubPanel.setLocation(SPACING, SPACING);
		clubEMailPanel.setLocation(SPACING + PANEL_SIZE_X_ENTRY + SPACING, SPACING);
		clubWebPanel.setLocation(SPACING + PANEL_SIZE_X_ENTRY + SPACING, SPACING + PANEL_SIZE_Y_ENTRY_THIRD);
		// clubWebPanel.setLocation(410, 0);
		this.add(clubPanel);
		this.add(clubEMailPanel);
		this.add(clubWebPanel);

		// finish init
		clubPanel.finishInit();
		clubEMailPanel.finishInit();
		clubWebPanel.finishInit();
	}

}
